package per.turtle.system.controller;

import cn.hutool.extra.servlet.ServletUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.constant.CommonConstants;
import per.turtle.common.core.utils.ServletUtils;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.system.entity.SysLoginInfo;
import per.turtle.system.service.ISysLoginInfoService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @author turtle
 */
@AllArgsConstructor
@Api(value = "loginInfo", tags = {"loginInfo_访问记录管理模块"})
@RequestMapping("loginInfo")
@RestController
public class SysLoginInfoController extends BaseController {

    private final ISysLoginInfoService loginInfoService;

    @ApiOperation(value = "新增访问记录", notes = "新增访问记录")
    @PostMapping("add")
    public CommonResult<Void> add(@RequestParam("username") String username, @RequestParam("status") String status, @RequestParam("message") String message) {
        String ip = ServletUtil.getClientIP(Objects.requireNonNull(ServletUtils.getRequest()));

        // 封装对象
        SysLoginInfo loginInfo = new SysLoginInfo();
        loginInfo.setUserName(username);
        loginInfo.setIpaddr(ip);
        loginInfo.setMsg(message);
        // 日志状态
        if (CommonConstants.LOGIN_SUCCESS.equals(status) || CommonConstants.LOGOUT.equals(status)) {
            loginInfo.setIsSuccess(true);
        } else if (CommonConstants.LOGIN_FAIL.equals(status)) {
            loginInfo.setIsSuccess(false);
        }
        return result(loginInfoService.insertLoginInfo(loginInfo));
    }

    @ApiOperation(value = "清空访问记录", notes = "清空访问记录")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("clean")
    @PreAuthorize(hasPermission = "system:loginInfo:remove")
    public CommonResult<Void> clean() {
        loginInfoService.cleanLoginInfo();
        return CommonResult.success();
    }

    @ApiOperation(value = "导出访问记录", notes = "导出访问记录")
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    @PreAuthorize(hasPermission = "system:loginInfo:export")
    @ResponseExcel("登录日志")
    public List<SysLoginInfo> export(HttpServletResponse response, @RequestBody SysLoginInfo loginInfo) {
        return loginInfoService.selectLoginInfoList(loginInfo);
    }

    @ApiOperation(value = "获取访问记录列表或分页", notes = "获取访问记录列表或分页（区别于是否传入分页参数）")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "system:loginInfo:list")
    public TableDataInfo<SysLoginInfo> list(@RequestBody SysLoginInfo loginInfo) {
        List<SysLoginInfo> list = startPage(s -> loginInfoService.selectLoginInfoList(loginInfo));
        return getDataTable(list);
    }

    @ApiOperation(value = "删除访问记录", notes = "删除访问记录")
    @DeleteMapping("{infoIds}")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "system:loginInfo:remove")
    public CommonResult<Void> remove(@PathVariable List<Long> infoIds) {
        return result(loginInfoService.deleteLoginInfoByIds(infoIds));
    }

}
